#include "typedef.h"
#include "astraldef.h"

/***************************************************
                    stack frame
    type0    type1
    [13]    [18]    others, such as fp,vector and so on
    [  ]    [17]    pc
    [12]    [16]    lr
    [11]    [15]    sp
    [10]    [14]    r12
    [ 9]    [13]    r11
    [ 8]    [12]    r10
    [ 7]    [11]    r9
    [ 6]    [10]    r8
    [ 5]    [ 9]    r7
    [ 4]    [ 8]    r6
    [ 3]    [ 7]    r5
    [ 2]    [ 6]    r4
    [  ]    [ 5]    r3
    [  ]    [ 4]    r2
    [  ]    [ 3]    r1
    [  ]    [ 2]    r0
    [ 1]    [ 1]    cpsr
    [ 0]    [ 0]    status
****************************************************/


StackPointer arch_build_stack_frame(StackPointer sp_top, address pc, object args)
{
    word *top = CAST_AS(word*, sp_top);
    *top-- = (word)pc;   // pc
    *top-- = 0xaabbccdd; // lr
    *top-- = 0xc;        // r12
    *top-- = 0xb;        // r11
    *top-- = 0xa;        // r10
    *top-- = 0x9;        // r9
    *top-- = 0x8;        // r8
    *top-- = 0x7;        // r7
    *top-- = 0x6;        // r6
    *top-- = 0x5;        // r5
    *top-- = 0x4;        // r4
    *top-- = 0x3;        // r3
    *top-- = 0x2;        // r2
    *top-- = 0x1;        // r1
    *top-- = (word)args; // r0 trial_task_ctrl(r0)
    *top-- = 0x0353;     // spsr to recover
    *top = 0x1;          // indicate full stack
    return top;
}

void arch_sync_task_status(AstralTask curr, AstralTask next)
{
    curr->released = True;
    while (!next->released)
    {
    }
    next->status = RUNNING;
    next->released = False;
    next->run_times++;
}